<script setup lang="ts">
import { useGuessList } from '@/composables'
import { useMemberStore } from '@/stores'
import { config } from '@/global'
// 获取屏幕边界到安全区域距离
const { safeAreaInsets } = uni.getSystemInfoSync()
// 订单选项
const orderTypes = [
  { id: '1', url: '/pages/exercise/index', text: '练习题', icon: 'icon-check' },
  { id: '2', url: '/pages/exercise/errorQuestions', text: '错题本', icon: 'icon-gift' },
  {
    id: '3',
    url: '/pages/points/exchangeRecord',
    text: '兑换记录',
    icon: 'icon-currency',
  },
]
// 获取会员信息
const memberStore = useMemberStore()

const { guessRef, onScrolltolower } = useGuessList()
</script>

<template>
  <scroll-view enable-back-to-top @scrolltolower="onScrolltolower" class="viewport" scroll-y>
    <!-- 个人资料 -->
    <view class="profile" :style="{ paddingTop: safeAreaInsets!.top + 'px' }">
      <!-- 情况1：已登录 -->
      <view class="overview" v-if="memberStore.profile">
        <navigator url="/pagesMember/profile/profile" hover-class="none">
          <image class="avatar" :src="memberStore.profile?.avatar" mode="aspectFill"></image>
        </navigator>
        <view class="meta">
          <view class="nickname">
            {{ memberStore.profile.userName }}
          </view>
          <view class="extra" hover-class="none" v-if="memberStore.profile.role === 'student'">
            <text class="update">积分：{{ memberStore.profile.points }}</text>
          </view>
        </view>
      </view>
      <!-- 情况2：未登录 -->
      <view class="overview" v-else>
        <navigator url="/pages/login/login" hover-class="none">
          <image
            class="avatar gray"
            mode="aspectFill"
            :src="config.baseURL + '/profile/upload/defaultAvatar.png'"
          ></image>
        </navigator>
        <view class="meta">
          <navigator url="/pages/login/login" hover-class="none" class="nickname">
            未登录
          </navigator>
          <view class="extra">
            <text class="tips">点击登录账号</text>
          </view>
        </view>
      </view>
      <navigator class="settings" url="/pagesMember/settings/settings" hover-class="none">
        设置
      </navigator>
    </view>
    <!-- 我的订单 -->
    <view class="orders">
      <view class="title"> 我的 </view>
      <view class="section">
        <!-- 订单 -->
        <navigator
          v-for="item in orderTypes"
          :key="item.id"
          :class="item.icon"
          :url="item.url"
          class="navigator"
          hover-class="none"
        >
          {{ item.text }}
        </navigator>
        <!-- 客服 -->
        <!-- #ifdef MP-WEIXIN -->
        <button class="contact icon-handset" open-type="contact">售后</button>
        <!-- #endif -->
      </view>
    </view>
    <!-- 猜你喜欢 -->
    <view class="guess">
      <XtxGuess ref="guessRef" />
    </view>
  </scroll-view>
</template>

<style lang="scss">
page {
  height: 100%;
  overflow: hidden;
  background-color: $bg-color;
}

.viewport {
  height: 100%;
  background-repeat: no-repeat;
  background-image: linear-gradient(135deg, $primary-color 0%, #64b5f6 100%);
  background-size: 100% 500rpx;
}
/* 用户信息 */
.profile {
  margin-top: 30rpx;
  position: relative;
  padding: 0 30rpx 40rpx;

  .overview {
    display: flex;
    height: 140rpx;
    padding: 0 6rpx;
    color: #fff;
    align-items: center;
  }

  .avatar {
    width: 140rpx;
    height: 140rpx;
    border-radius: 50%;
    background-color: #eee;
    border: 6rpx solid rgba(255, 255, 255, 0.3);
    box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.1);
  }

  .gray {
    filter: grayscale(100%);
  }

  .meta {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: flex-start;
    line-height: 40rpx;
    padding: 16rpx 0;
    margin-left: 30rpx;
  }

  .nickname {
    max-width: 280rpx;
    margin-bottom: 10rpx;
    font-size: 36rpx;
    font-weight: 600;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .extra {
    display: flex;
    font-size: 24rpx;
  }

  .tips {
    font-size: 24rpx;
    color: rgba(255, 255, 255, 0.8);
  }

  .update {
    padding: 6rpx 16rpx 4rpx;
    color: rgba(255, 255, 255, 0.9);
    border: 1rpx solid rgba(255, 255, 255, 0.8);
    margin-right: 10rpx;
    border-radius: 30rpx;
    font-size: 22rpx;
  }

  .settings {
    position: absolute;
    bottom: 40rpx;
    right: 40rpx;
    font-size: 30rpx;
    color: #fff;
  }
}

/* 我的订单 */
.orders {
  position: relative;
  z-index: 99;
  padding: 30rpx;
  margin: 20rpx 24rpx 0;
  background-color: $card-bg;
  border-radius: $border-radius;
  box-shadow: $shadow;

  .title {
    height: 40rpx;
    line-height: 40rpx;
    font-size: 32rpx;
    color: $text-main;
    font-weight: 600;
    margin-bottom: 30rpx;
  }

  .section {
    width: 100%;
    display: flex;
    justify-content: space-between;
    padding: 0 10rpx;

    .navigator,
    .contact {
      display: flex;
      flex-direction: column;
      align-items: center;
      font-size: 26rpx;
      color: $text-secondary;
      width: 140rpx;

      &::before {
        display: block;
        font-size: 60rpx;
        color: $primary-color;
        margin-bottom: 16rpx;
      }

      &::after {
        border: none;
      }

      &:active {
        opacity: 0.8;
      }
    }

    .contact {
      padding: 0;
      margin: 0;
      border: 0;
      background-color: transparent;
      line-height: inherit;
    }
  }
}

/* 猜你喜欢 */
.guess {
  background-color: $bg-color;
  margin-top: 24rpx;
  padding: 0 24rpx;
}
</style>
